package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.UserTherapistRelation;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IUserTherapistRelationService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

//import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户陪诊关联Controller
 *
 * @author ruoyi
 * @date 2025-06-10
 */
@RestController
@RequestMapping("/system/relation")
public class UserTherapistRelationController extends BaseController
{
    @Autowired
    private IUserTherapistRelationService userTherapistRelationService;
    @Autowired
    private ISysUserService iSysUserService;
    /**
     * 查询用户陪诊关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(required = false) Long userId,
                              @RequestParam(required = false) Long therapistId,
                              UserTherapistRelation userTherapistRelation)
    {
        startPage();
        List<UserTherapistRelation> list = userTherapistRelationService.selectUserTherapistRelationList(userTherapistRelation);
        return getDataTable(list);
    }

    /**
     * 导出用户陪诊关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:relation:export')")
    @Log(title = "用户陪诊关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserTherapistRelation userTherapistRelation)
    {
        List<UserTherapistRelation> list = userTherapistRelationService.selectUserTherapistRelationList(userTherapistRelation);
        ExcelUtil<UserTherapistRelation> util = new ExcelUtil<UserTherapistRelation>(UserTherapistRelation.class);
        util.exportExcel((jakarta.servlet.http.HttpServletResponse) response, list, "用户陪诊关联数据");
    }
    @GetMapping("/userId")
    public AjaxResult getUserIdByUserName(String username) {
        SysUser user = iSysUserService.selectUserByUserName(username);
        if (user == null) {
            return AjaxResult.error("用户不存在");
        }
        return AjaxResult.success(user.getUserId());
    }
    /**
     * 获取用户陪诊关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:relation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userTherapistRelationService.selectUserTherapistRelationById(id));
    }

    /**
     * 新增用户陪诊关联
     */
    @PreAuthorize("@ss.hasPermi('system:relation:add')")
    @Log(title = "用户陪诊关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserTherapistRelation userTherapistRelation)
    {
        return toAjax(userTherapistRelationService.insertUserTherapistRelation(userTherapistRelation));
    }

    /**
     * 修改用户陪诊关联
     */
    @PreAuthorize("@ss.hasPermi('system:relation:edit')")
    @Log(title = "用户陪诊关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserTherapistRelation userTherapistRelation)
    {
        return toAjax(userTherapistRelationService.updateUserTherapistRelation(userTherapistRelation));
    }

    /**
     * 删除用户陪诊关联
     */
    @PreAuthorize("@ss.hasPermi('system:relation:remove')")
    @Log(title = "用户陪诊关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userTherapistRelationService.deleteUserTherapistRelationByIds(ids));
    }
}
